﻿using System;
using System.Windows;
using System.Windows.Interactivity;
using System.Windows.Media.Animation;
using PairingMatrix.ViewModel;

namespace PairingMatrix.View.Behavior
{
    public class OpacityForMingleCardBehavior : Behavior<MingleCardControl>
    {
        private MingleCardViewModel _mingleCardViewModel;
        private Storyboard _storyboardOpacity;
        private DoubleAnimation _opacityAnimation;

        protected override void OnAttached()
        {
            AssociatedObject.Loaded += (sender, e) =>
            {
                _mingleCardViewModel = (MingleCardViewModel)AssociatedObject.DataContext;
                _mingleCardViewModel.PropertyChanged += MingleCardPropertyChanged;

                //animation for pairwith controls
                _opacityAnimation = new DoubleAnimation();
                TimeSpan duration = TimeSpan.FromMilliseconds(500);
                _opacityAnimation.Duration = duration;
                _storyboardOpacity = new Storyboard();
                _storyboardOpacity.Duration = duration;
                _storyboardOpacity.Children.Add(_opacityAnimation);
                Storyboard.SetTarget(_opacityAnimation, AssociatedObject.CardBorder);
                Storyboard.SetTargetProperty(_opacityAnimation, new PropertyPath("Opacity"));

            };

        }

        void MingleCardPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "IsCardVisibleInCurrentWall")
            {
                if (_mingleCardViewModel.IsCardVisibleInCurrentWall)
                {
                    _opacityAnimation.To = 1.0;
                    _storyboardOpacity.Begin();
                }
                else
                {
                    _opacityAnimation.To = 0.0;
                    _storyboardOpacity.Begin();
                }
                
            }
        }

    }
}
